package reflect_ex_1;

import reflect.annotations.AutoRunClass;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 实例化与当前类Test3在同一个包中被@AutoRunClass标注的类
 */
public class Test31 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException {
        File dir = new File(
                Test31.class.getResource(".").toURI()
        );
        System.out.println(dir);

        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File sub:subs){
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.lastIndexOf("."));
            String packName = Test31.class.getPackage().getName();
            className = packName + "."+ className;
            Class cls = Class.forName(className);
            if(cls.isAnnotationPresent(AutoRunClass.class)){
                Object obj = cls.newInstance();
                System.out.println(obj);
            }
        }
    }
}
